package com.tdam2011.grupo04;


import java.util.Date;

import com.tdam2011.grupo04.SMSWeb.MensajeWeb;
import com.tdam2011.grupo04.entidades.Accion;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * A wrapper for a broadcast receiver that provides network connectivity state
 * information, independent of network type (mobile, Wi-Fi, etc.). {@hide
 * }
 */
public class ConnectivityBroadcastReceiver extends BroadcastReceiver {
		
		private static final String TAG = "ConnectivityBroadcastReceiver";
		/** Network connectivity information */
		private NetworkInfo mNetworkInfo;

		/**
		 * In case of a Disconnect, the connectivity manager may have already
		 * established, or may be attempting to establish, connectivity with another
		 * network. If so, {@code mOtherNetworkInfo} will be non-null.
		 */
		private NetworkInfo mOtherNetworkInfo;
		
		@Override
		public void onReceive(Context context, Intent intent) {
			
			mNetworkInfo = (NetworkInfo) intent
			.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
			
//			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
			if(mNetworkInfo != null)
			{
				Accion accion = new Accion();
				
//				boolean noConnectivity = intent.getBooleanExtra(
//						ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
				
				
//				mOtherNetworkInfo = (NetworkInfo) intent
//				.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
//								
//				if (noConnectivity) {
//					accion.setEstado("Desconectado");
//					
//				} else {
//					accion.setEstado("Conectado");
//				}
				accion.setEstado(mNetworkInfo.getState().toString());
				accion.setTipo(mNetworkInfo.getTypeName());
				Date d = new Date();
				accion.setFecha(d.toString());
				
				Log.w(TAG, "onReceived() called with " + accion.toString()
						+ " and " + intent);
				ArrayAcciones.addAccion(accion);
				
				
				// Si el estado es Conectado activo en envio de mensajes web
				if(mNetworkInfo.getState().toString().equals("CONNECTED"))
					MensajeWeb.hayConectividad= true;
				else
					MensajeWeb.hayConectividad= false;
			}
		}
}